<?
/*
 * Copyright 2004-2005 Sigve Indregard.
 *
 * This file is part of Laivsys.
 *
 * Laivsys is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Laivsys is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Laivsys; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Id: evskalaer.php 6 2005-07-11 23:14:47Z say $
 */
 
include("../db.php");
include("auth.php");
include("top.php");
include("evskjemainclude.php");
evskjemameny();
?>
<h1>Evalueringsskalaer</h1>
<?

//this page has got three modes: either look at all scales (default),
//add new scale or edit existing scale

if ($_REQUEST['mode'] == 'add') {
	if ($_REQUEST['phase'] == '2') {
		$sql = "INSERT INTO r_evskalaer (Navn) VALUES ('{$_REQUEST['navn']}')";
		mysql_query($sql) or die(mysql_error());
		$sid=mysql_insert_id();

		foreach ($_REQUEST['verdi'] as $id=>$verdi) {
			$sql="INSERT INTO r_evskalaitem (SkalaID, Verdi, Tekst) VALUES ($sid, $verdi, '{$_REQUEST['tekst'][$id]}')";
			mysql_query($sql);
		}
		printscales();
	} else {
		addscale($_REQUEST['items']);
	}
} elseif ($_REQUEST['mode'] == 'edit') {
	editscale($_REQUEST['id']);
} elseif ($_REQUEST['mode'] == 'delete') {
	$id = $_REQUEST['id'];
	dropscale($id);
} elseif ($_REQUEST['mode'] == 'update') {
	$id = $_REQUEST['id'];
	$navn = $_REQUEST['navn'];
	$items = $_REQUEST['item'];

	$sql = "UPDATE r_evskalaer SET Navn='$navn' WHERE ID=$id";
	mysql_query($sql);
	foreach ($items as $iid=>$values) {
		$verdi=$values['value'];
		$tekst=$values['text'];
		$sql="UPDATE r_evskalaitem SET Verdi=$verdi, Tekst='$tekst' WHERE ID=$iid AND SkalaID=$id";
		mysql_query($sql);
	}
	printscales();
} else {
	printscales();
}

include("bottom.php");

function addscale($numitems) {
	echo "<h2>Legg til skala</h2>\n";
	echo "<form method=\"post\" action=\"evskalaer.php\">\n";
	echo "\t<input type=\"hidden\" name=\"mode\" value=\"add\">\n";
	echo "\t<input type=\"hidden\" name=\"phase\" value=\"2\">\n";

	echo "\t<table class=\"gridded\">\n";
	echo "\t\t<tr>\n";
	echo "\t\t\t<th>Navn</th>\n";
	echo "\t\t\t<td><input name=\"navn\"></td>\n";
	echo "\t\t</tr>\n";

	echo "\t\t<tr>\n";
	echo "\t\t\t<th>Alternativer</th>\n";
	echo "\t\t\t<td><table class=\"gridded\">\n";
	echo "\t\t\t\t<tr><th>Verdi</th><th>Tekst</th></tr>\n";
	for ($i=1; $i <= $numitems; $i++) {
		echo "\t\t\t\t<tr><td><input name=\"verdi[$i]\" value=\"$i\" size=\"4\"></td><td><input name=\"tekst[$i]\"></td></tr>\n";
	}
	echo "\t\t\t</table></td>\n";
	echo "\t\t</tr>\n";
	echo "\t</table>\n";

	echo "\t<input type=\"submit\" value=\"Lagre\">\n";
	echo "</form>";
}
function editscale($id) {
	$sql = "SELECT * FROM r_evskalaer WHERE ID=$id";
	$rs = mysql_query($sql);
	if (mysql_num_rows($rs)) {
		$skala = mysql_fetch_assoc($rs);
		echo "<h2>Rediger skala</h2>\n";
		echo "<form method=\"post\" action=\"evskalaer.php\">\n";
		echo "\t<input type=\"hidden\" name=\"mode\" value=\"update\"/>\n";
		echo "\t<p><b>Navn:</b><br/>\n";
		echo "\t<input name=\"navn\" value=\"{$skala['Navn']}\"/></p>\n";
		$sql = "SELECT * FROM r_evskalaitem WHERE SkalaID=$id ORDER BY Verdi ASC";
		$items = mysql_query($sql);
		$i=0;
		echo "<table class=\"gridded\">\n";
		echo "<tr><th>Alt.</th><th>Verdi</th><th>Tekst</th></tr>\n";
		while ($item =mysql_fetch_assoc($items)) {
			$iid = $item['ID'];
			$i++;
			echo "\t<tr><th>Alternativ $i</th>\n";
			echo "\t<td><input name=\"item[$iid][value]\" size=\"4\"
					value=\"{$item['Verdi']}\"/></td>\n";
			echo "\t<td><input name=\"item[$iid][text]\"
					value=\"{$item['Tekst']}\"/></td>\n";
			echo "\t</tr>\n";
		}
		echo "</table>\n";
		echo "<input type=\"submit\" value=\"Oppdater\"/>\n";
		echo "</form>\n";
	} else {
		echo "Ingen slik skala ($id)";
	}
}
function printscales() {
	$sql="SELECT * FROM r_evskalaer ORDER BY Navn";
	$skalaer=mysql_query($sql);
	echo "<table border=\"1\" class=\"gridded\">";
	while ($skala = mysql_fetch_assoc($skalaer)) {
		$id=$skala['ID'];
		?>
		<tr>
			<th><?=$skala['Navn']?></th>
			<td><a href="evskalaer.php?mode=edit&amp;id=<?=$id?>">[rediger]</a></td>
			<td><a href="evskalaer.php?mode=delete&amp;id=<?=$id?>">[slett]</a></td>
		</tr>
		<?
	}
	echo "</table>";

	echo "<hr/>";
	echo "<h2>Ny skala</h2>";
	echo "<form method=\"get\" value=\"evskalaer.php\"/>";
	echo "<input type=\"hidden\" name=\"mode\" value=\"add\"/>";
	echo "<b>Antall alternativer:</b><br/>";
	echo "<input name=\"items\" size=\"4\"/><br/>";
	echo "<input type=\"submit\" value=\"Lag!\">";
	echo "</form>";
}

function dropscale ($id) {
	$sql = "SELECT * FROM r_evsporsmal WHERE Skala=$id";
	$rs = mysql_query($sql);
	if (mysql_num_rows($rs)) {
		$num = mysql_num_rows($rs);
		echo "<p>Skalaen kan ikke slettes, den er i bruk av
				$num sp�rsm�l.</p>";
	} else {
		$sql = "DELETE FROM r_evskalaer WHERE ID=$id";
		mysql_query($sql);
		$sql = "DELETE FROM r_evskalaitem WHERE SkalaID=$id";
		mysql_query($sql);
	}
	printscales();
}

?>
